####Autocratic Revolving Doors####
####Roman-Gabriel Olar####
####Created on 17/02/2022####
####Last modified on 12/10/2024####

# Load the packages
library(ggplot2)
library(foreign)
library(lme4)
library(broom)
library(tidyverse)
library(lubridate)
library(haven)
library(survival)
library(plm)
library(MASS)
library(mfx)
library(dummy)
library(prediction)
library(margins)
library(brglm)
library(psych)
library(logistf)
library(stargazer)
library(huxtable)
library(haven)           

rm(list = ls())
setwd("C:/Users/Administrator/Dropbox/Papers/ARD Elites/Draft/ARD Observational/PoP/Final submission/Data")

#Import data   
TSCS <- read_dta("MainData_ARD_PoP.dta")
CS <- read_dta("MainData_ARD_PoP_CS.dta")

###Subset datasets###

CS1<-subset(CS, app_elec<2)

TSCS1 <- subset(TSCS, app_elec < 2) 

CS2 <- subset(CS,app_elec!=1) 

TSCS2 <- subset(TSCS, app_elec!=1) 

###Lower data
rm(CS, TSCS)

####Appointment to cabinet####

#####CS DATA######
#MODEL 1 CS
app1_cs <- brglm(appointed ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = CS1)
summary(app1_cs)


#MODEL 2 CS
app2_cs <- brglm(appointed ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + female +
                   dem_const + coldwar  + prev_gwf_military + prev_gwf_party + prev_gwf_personal + 
                   lrgdpopct1 + rgdpopc_1yrcht1 + litpopt1 +
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = CS1)
summary(app2_cs)

##MODEL 3 TSCS
app1_tscs <- brglm(appointed ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS1)
summary(app1_tscs)

#MODEL 4 TSCS
app2_tscs <- brglm(appointed ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                   yr_t + yr_since_exit +
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS1)
summary(app2_tscs)

#MODEL 5 TSCS
app3_tscs <- brglm(appointed ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                   female +
                   dem_const + coldwar + prev_gwf_military + prev_gwf_party + prev_gwf_personal + 
                   lrgdpopct1 + rgdpopc_1yrcht1 + litpopt1 +
                   yr_t + yr_since_exit +
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS1)
summary(app3_tscs)



################################
######ELECTED###################
################################


#####CS DATA######
#MODEL 1 CS
elec1_cs <- brglm(elected ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = CS2)
summary(elec1_cs)


#MODEL 2 CS
elec2_cs <- brglm(elected ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + female +
                   dem_const + coldwar + prev_gwf_military + prev_gwf_party + prev_gwf_personal + 
                   lrgdpopct1 + rgdpopc_1yrcht1 + litpopt1 +
                   factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = CS2)
summary(elec2_cs)

##MODEL 3 TSCS
elec1_tscs <- brglm(elected ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                     factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS2)
summary(elec1_tscs)

#MODEL 4 TSCS
elec2_tscs <- brglm(elected ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                     yr_t + yr_since_exit +
                     factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS2)
summary(elec2_tscs)

#MODEL 5 TSCS
elec3_tscs <- brglm(elected ~ prev_core + NUML_aut + prev_prestige_2 + prev_military + 
                     female +
                     dem_const + coldwar + prev_gwf_military + prev_gwf_party + prev_gwf_personal + 
                     lrgdpopct1 + rgdpopc_1yrcht1 + litpopt1 +
                     yr_t + yr_since_exit +
                     factor(gwf_caseid), family=binomial(link="logit"), method = "brglm.fit", p1 = T, data = TSCS2)
summary(elec3_tscs)


setwd("C:/Users/Administrator/Dropbox/Papers/ARD Elites/Data/Tables/July24")

####APPOINTED EXPORT####
stargazer(app1_cs,
          style = "apsr", no.space = TRUE,
          out = "app_pmle1.html")

stargazer(app2_cs,
          style = "apsr", no.space = TRUE,
          out = "app_pmle2.html")

stargazer(app1_tscs,
          style = "apsr", no.space = TRUE,
          out = "app_pmle3.html")

stargazer(app2_tscs,
          style = "apsr", no.space = TRUE,
          out = "app_pmle4.html")

stargazer(app3_tscs,
          style = "apsr", no.space = TRUE,
          out = "app_pmle5.html")


###Elected export
stargazer(elec1_cs,
          style = "apsr", no.space = TRUE,
          out = "elec_pmle1.html")

stargazer(elec2_cs,
          style = "apsr", no.space = TRUE,
          out = "elec_pmle2.html")

stargazer(elec1_tscs,
          style = "apsr", no.space = TRUE,
          out = "elec_pmle3.html")

stargazer(elec2_tscs,
          style = "apsr", no.space = TRUE,
          out = "elec_pmle4.html")

stargazer(elec3_tscs,
          style = "apsr", no.space = TRUE,
          out = "elec_pmle5.html")
